package searchTree;

import java.util.ArrayList;
import java.util.Map;

import phylogenetictree.*;

public class searchTreeNode implements Comparable<searchTreeNode> {
	Forest myForest; 
	int numberOfCuts;
	Map<Integer,Integer> translation;
	
	
	private ArrayList<String> edgesCut;

	// controlling the phase
	private int phase;
	public static int phaseOne = 1;
	public static int phaseTwo = 2;
	
	public searchTreeNode(PhylogeneticTree tree)
	{
		this.myForest = new Forest(tree);
		this.edgesCut = new ArrayList<String>();
		this.numberOfCuts = 0;
	}
	
	public void removeEdge(Integer a,Integer b)
	{
		if(Settings.debugging)
		{
			edgesCut.add(myForest.getTree().indexMap.get(a) + "," + myForest.getTree().indexMap.get(b));
		}
		this.myForest.removeEdge(a, b);
		this.numberOfCuts++;
	}
	
	public Map<Integer, Integer> getTranslation() {
		return translation;
	}

	public void setTranslation(Map<Integer, Integer> translation) {
		this.translation = translation;
	}
	
	public Forest getForest() {
		return myForest;
	}
	
	public PhylogeneticTree getTree()
	{
		return this.myForest.getTree();
	}

	public int getNumberOfCuts() {
		return numberOfCuts;
	}

	public void setPhase(int phase) {
		this.phase = phase;
	}

	public int getPhase() {
		return phase;
	}

	public void cutBranch(Map<Integer, Map<Integer, String>> pathMap,
			Integer toCut, Integer junction) {
		String path = pathMap.get(junction).get(toCut);
		
		String[] pathSplit = path.split(",");
		int k = 1;
		Integer test = Integer.valueOf(pathSplit[k]);
		while(!this.myForest.getTree().DoesNodeExcist(test))
		{
			k++;
			test = Integer.valueOf(pathSplit[k]);
		}		
		this.numberOfCuts++;
		edgesCut.add("Junction : " + this.myForest.getTree().indexMap.get(junction) + " edge with : " + this.myForest.getTree().indexMap.get(pathSplit[k]) + " was cut");
		
		myForest.removeEdge(junction, Integer.valueOf(pathSplit[k]));
	}

	@Override
	public int compareTo(searchTreeNode other) {
		int one = this.getNumberOfCuts();
		int two = other.getNumberOfCuts();
		if( one > two)
		{
			return 1;
		}
		else if (two > one)
		{
			return -1;
		}
		else
		{
			return 0;
		}
	}
}
